package linuxbox.poll.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import linuxbox.BoxStatic;
import linuxbox.bean.*;
import linuxbox.common.*;

import linuxbox.poll.logic.*;

public class BoxPollConcludeAction extends Action
{
	protected static String viewJSP = null;
	protected static String concludeJSP = null;
  protected static TimerTask task = null;

  static BoxPoll home = new BoxPoll();
  public BoxPollConcludeAction()
  {
    if (task == null)
    {
      task = new BoxPollAutoConclude(home);
      //Set the first time on the hour, and then check each hour
      Calendar today = Calendar.getInstance();
      today.add(Calendar.HOUR, 1);
      today.set(Calendar.MINUTE,0);
      today.set(Calendar.SECOND,0);

//      BoxStatic.timer.schedule(task, 2000, 10000);
      BoxStatic.timer.schedule(task, today.getTime(), 1000*3600 );
    }
  }

  public void doAction(HttpSession aSession, BoxSession aBoxSession,
  HttpServletRequest req, HttpServletResponse resp) throws Exception
	{
    String str = req.getParameter("SubmitCancel");
    if ( str != null )
    {
      sendFile(viewJSP,req,resp);
      return;
    }

    int curEntryID = 0;
    str = req.getParameter("ID");
    if ( str != null)
    {
      curEntryID = Integer.parseInt(str);
    }
    str = req.getParameter("SubmitView");
    if ( str != null )
    {
      viewResults(curEntryID, req, resp);
      return;
    }
    str = req.getParameter("SubmitConclude");
    if ( str != null )
    {
      doConclude(curEntryID, req, aBoxSession);

      sendFile(viewJSP,req,resp);
      return;
    }
  }

  public boolean doConclude(int id, HttpServletRequest req, BoxSession aBoxSession)
  {
    //TODO: check user access rights
    String userID = aBoxSession.getLoginID();

    int sendresult = 0; //default to don't send any results
    String str = req.getParameter("SENDRESULT");
    if ( str != null)
    {
      sendresult = Integer.parseInt(str);
    }
    return home.doConclude(id, sendresult);
  }

  public void viewResults(int id, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
  {
    if ( id == 0 )
      return;
    BoxPollEntry entry = home.get(new BaseIntKey(id));
    if ( entry == null )
    {
      //TODO: Log error
      return;
    }
    if ( entry.getClosed() )
    {
      //Shouldn't come here
      sendFile(viewJSP,req,resp);
      return;
    }

    req.setAttribute("ID", new Integer(id).toString());
    req.setAttribute("SHOWVOTER", entry.getShowVoter()?"1":"0");
    req.setAttribute("SUBJECT", entry.getSubject());
    req.setAttribute("DESCRIPTION", entry.getDescription());
    req.setAttribute("SENDRESULT", new Integer(entry.getSendResult()).toString());

    sendFile(concludeJSP,req,resp);
  }

	public void setViewJSP(String str)
	{
		viewJSP = str;
	}
	public void setConcludeJSP(String str)
	{
		concludeJSP = str;
	}
}
